package com.onenine.app.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.PagerSnapHelper;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.onenine.app.R;
import com.onenine.app.utils.SpCommonHelper;
import com.onenine.base.base.BaseActivity;
import com.onenine.base.utils.StatusBarUtils;
import com.onenine.base.view.CircleIndicatorView;
import com.onenine.common.util.ActivityUtils;

import java.util.ArrayList;

/**
 * 引导页面
 */
public class GuideActivity extends BaseActivity {

    private final int[] mGuideResIds = {R.drawable.ic_guide_first, R.drawable.ic_guide_second
            , R.drawable.ic_guide_three, R.drawable.ic_guide_four};
    private RecyclerView mRvView;
    private CircleIndicatorView mIndicatorView;
    private PagerSnapHelper mSnapHelper;
    private int mCurrentPosition;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        StatusBarUtils.setTransparent(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_guide;
    }

    @Override
    protected void setStatusBarStyle() {
    }

    @Override
    protected void initViews() {
        super.initViews();
        mRvView = findViewById(R.id.rv_preview);
        mIndicatorView = findViewById(R.id.civ_indicator);
        findViewById(R.id.btn_start).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                launchNextActivity();
            }
        });
        mSnapHelper = new PagerSnapHelper() {
            @Override
            public int findTargetSnapPosition(RecyclerView.LayoutManager layoutManager, int velocityX, int velocityY) {
                int position = super.findTargetSnapPosition(layoutManager, velocityX, velocityY);
//                if (position == mGuideResIds.length) {
//                    launchNextActivity();
//                    return position;
//                }
                if (position > -1 && position < mGuideResIds.length) {
                    mCurrentPosition = position;
                    mIndicatorView.playTo(mGuideResIds.length, mCurrentPosition);
                }
                return mCurrentPosition;
            }
        };
        mSnapHelper.attachToRecyclerView(mRvView);
        ImageAdapter imgAdapter = new ImageAdapter();
        mRvView.setLayoutManager(new LinearLayoutManager(this
                , LinearLayoutManager.HORIZONTAL, false));
        mRvView.setAdapter(imgAdapter);
        imgAdapter.bindToRecyclerView(mRvView);
        mRvView.scrollToPosition(mCurrentPosition);
        mIndicatorView.playTo(mGuideResIds.length, mCurrentPosition);
        ArrayList imgList = new ArrayList();
        for (int i = 0; i < mGuideResIds.length; i++) {
            imgList.add(mGuideResIds[i]);
        }
        imgAdapter.addData(imgList);
    }

    /**
     * 加载下一页
     */
    private void launchNextActivity() {
        SpCommonHelper.getInstance().setGuide();
        ActivityUtils.startActivity(GuideActivity.this, LoginActivity.class);
        finish();
    }

    private class ImageAdapter extends BaseQuickAdapter<Integer, BaseViewHolder> {

        public ImageAdapter() {
            super(R.layout.layout_guide_item);
        }

        @Override
        protected void convert(BaseViewHolder helper, Integer item) {
            ImageView imageView = (ImageView) helper.getConvertView();
            imageView.setImageResource(item);
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    if (helper.getPosition() == mGuideResIds.length - 1) {
//                        launchNextActivity();
//                    }
                }
            });
        }
    }

}
